package com.jn.erp.web.api.course.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.common.enums.ErrorEnum;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.THoliday;
import com.jn.erp.web.api.course.entity.vo.THolidayVo;
import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.course.mapper.THolidayMapper;
import com.jn.erp.web.api.course.service.THolidayService;
import com.jn.erp.web.api.course.task.TCourseOrderTask;
import org.apache.poi.ss.formula.functions.T;
import org.apache.tomcat.jni.Error;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-12-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class THolidayServiceImpl extends ServiceImpl<THolidayMapper, THoliday> implements THolidayService {
    @Autowired
    private TCourseOrderMapper tCourseOrderMapper;
    @Autowired
    private TCourseOrderTask tCourseOrderTask;
    @Override
    public int saveHolidayOrder(THolidayVo tHolidayVo) {
        //校验同一时间段不能有两个假期
        if(0 == tHolidayVo.getState()) {
            Long aLong = baseMapper.selectCount(new QueryWrapper<THoliday>().eq("is_delete", 0).eq("state", 0).eq("org_id",tHolidayVo.getOrgId()).and(e -> e.between("start_date", tHolidayVo.getStartDate(), tHolidayVo.getEndDate()).or().between("end_date", tHolidayVo.getStartDate(), tHolidayVo.getEndDate())));
            if(aLong > 0L){
                throw new RuntimeException(ErrorEnum.E111.getName());
            }
        }
        int i = baseMapper.insert(tHolidayVo);
        //课时删除
        if(0 == tHolidayVo.getState()) {
            //课时删除
            deleteCourseOrder(tHolidayVo);
        }
        return i;
    }

    @Override
    public int updateHolidayOrder(THolidayVo tHolidayVo) {
        if(!"1".equals(tHolidayVo.getIsDelete()) && 0 == tHolidayVo.getState() ){
            //校验同一时间段不能有两个假期
            Long aLong = baseMapper.selectCount(new QueryWrapper<THoliday>().eq("is_delete", 0).eq("state", 0).eq("org_id",tHolidayVo.getOrgId()).and(e -> e.between("start_date", tHolidayVo.getStartDate(), tHolidayVo.getEndDate()).or().between("end_date", tHolidayVo.getStartDate(), tHolidayVo.getEndDate())).ne("id",tHolidayVo.getId()));
            if(aLong > 0L){
                throw new RuntimeException(ErrorEnum.E111.getName());
            }
        }
        int i = baseMapper.updateById(tHolidayVo);
        if(!"1".equals(tHolidayVo.getIsDelete()) && 0 == tHolidayVo.getState()) {
            //课时删除
            deleteCourseOrder(tHolidayVo);
        }
        return i;
    }

    @Override
    public List<THoliday> holidayList(THolidayVo tHolidayVo) {
        List<THoliday> tHolidays = baseMapper.selectList(new QueryWrapper<THoliday>().eq("org_id",tHolidayVo.getOrgId()).eq("is_delete",0));
        return tHolidays;
    }

    private void deleteCourseOrder(THolidayVo tHolidayVo){
        TCourseOrder tCourseOrder = new TCourseOrder();
        tCourseOrder.setIsDelete("1");
        tCourseOrder.setUpdateUser(Objects.isNull(tCourseOrder.getUpdateUser())? tHolidayVo.getCreateUser():tHolidayVo.getUpdateUser());
        tCourseOrder.setUpdateDate(LocalDateTime.now());
        int update = tCourseOrderMapper.update(tCourseOrder,new QueryWrapper<TCourseOrder>().eq("org_id",tHolidayVo.getOrgId()).between("DATE_FORMAT(class_date, '%Y-%m-%d' )", tHolidayVo.getStartDate(), tHolidayVo.getEndDate()));
        //开线程池，线程数量为要遍历的对象的长度
        ExecutorService executor = Executors.newFixedThreadPool(1);
        CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
            Date ss = new Date();
            tCourseOrderTask.createCourseOrder();
            Date ee = new Date();
            System.out.printf("创建排课记录执行时间：%d 毫秒." , (ee.getTime() - ss.getTime()));
        },executor);
    }
}
